﻿
using System;
using System.Collections.Generic;
namespace sharpxbot.Core.Security.Traffic.Counters
{
    /// <summary>
    /// Represents year counters. Contains array of 12 month counters
    /// </summary>
    public class YearCounters : BaseCounters
    {
        /// <summary>
        /// Gets year value
        /// </summary>
        public int Year { get; private set; }

        /// <summary>
        /// Gets array of month counters
        /// </summary>
        public MonthCounters[] Months { get; private set; }

        /// <summary>
        /// Creates new year counters using given year and month counters array
        /// </summary>
        /// <param name="year">Must be positive</param>
        /// <param name="months">Array of month counters. Must be valid array of 12 items</param>
        public YearCounters(int year, MonthCounters[] months)
            :base()
        {
            Validator.ValidateYear(year);

            if (months == null)
                throw new NullReferenceException("Illegal month counters array. Null-reference");

            if (months.Length < Constants.MonthsCount)
                throw new ArgumentException(String.Format("Illegal month counters array. Must be {0} length", Constants.MonthsCount));

            this.Year = year;
            this.Months = months;
        }

        /// <summary>
        /// Creates new year counters using given year value and initializes month counters array
        /// </summary>
        /// <param name="year">Must be positive</param>
        public YearCounters(int year)
            :base()
        {
            Validator.ValidateYear(year);

            this.Year = year;

            InitMonths();
        }

        /// <summary>
        /// Creates new year counters using current system time and initializes month counters array
        /// </summary>
        public YearCounters()
            :base()
        {
            this.Year = DateTime.Now.Year;

            InitMonths();
        }

        #region Private methods

        private void InitMonths()
        {
            List<MonthCounters> months = new List<MonthCounters>();

            for (int i = 0; i < Constants.MonthsCount; ++i)
            {
                months.Add(new MonthCounters(i));
            }

            this.Months = months.ToArray();
        }

        #endregion
    }
}
